<?php
namespace app\manage\model;
use think\Db;
use think\Model;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/8/5
 * Time: 16:25
 */
class Shops extends Model
{
    /**
     * 商铺管理
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function shopsList($matching=''){
        $data = input();
        $where['create_id'] = create_id($data['token']);
        //单元楼数据
        $data=Db::name('unit_building')
            ->field('tung_num,unit_num,layer_num,floor_type')
            ->where(['u_type'=>1])
            ->where($where)
            ->order('tung_num asc,unit_num asc')
            ->select();
        if($data!=null){
            foreach($data as $k=>$v){
                $data[$k]['unit']=$v['tung_num'].'-'.$v['unit_num'];
            }
        }
        //后台朝向数据
        $date=Db::name('settings')
            ->where(['apartment_state'=>1])
            ->where($where)
            ->field('housing_oorientation')
            ->find();
        //配套
        $i=Db::name('settings')
            ->where($where)
            ->where(['apartment_state'=>1])
            ->field('shop_matching')
            ->find();
        //模板
        $m=Db::name('shops_template')
            ->where($where)
            ->field('shops_t_id,s_te_name')
            ->where(['s_te_state'=>1])
            ->select();
        //经纪人
        //经纪人
        $user_id=Db::name('role')
            ->where(['status'=>1])
            ->where(['create_id'=>$where['create_id']])
            ->where(['name'=>'经纪人'])
            ->find();
        $user=Db::name('admin_user')
            ->where(['role_id'=>$user_id['role_id']])
            ->where(['admin_status'=>1])
            ->where(['create_id'=>$where['create_id']])
            ->field('admin_id,name')
            ->select();
        /*$user=Db::name('admin_user')
            ->where($where)
            ->where(['role_id'=>45])
            ->where(['admin_status'=>1])
            ->field('admin_id,name')
            ->select();*/
        //var_dump($m,$date);exit;
        //判断设置没有   没有返回2
        if($data!=null && $date!=null && $i!=null){
            $arr['orientation']=$date['housing_oorientation'];//朝向
            $arr['unit']=$data;//单元
         //   var_dump($matching,$i['shop_matching']);exit;
            if($i['shop_matching']!=null){
                if($matching!=null){
                    $arr1= explode(',',$i['shop_matching']);
                    $arr2= explode(',',$matching);
                 //   var_dump($arr1,$arr2);exit;
                    $result=array_merge($arr1,$arr2);
                    $result= array_unique($result);
                    $result= implode(',',$result);

                }else{
                    $result= $i['shop_matching'];
                }
            }else{
                if($matching!=null){
                    $result=$matching;
                }else{
                    $result= $i['shop_matching'];
                }

            }
         //   var_dump($result);exit;
            $arr['matching']=$result;//配套
            $arr['template']=$m;//模板
            $arr['user']=$user;//模板
            return $arr;
        }else{
            return 2;
        }
    }
    /**
     * 住房房源添加
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function shopsAdd($data){
        //处理数据
        $arr['s_tung_num'] = isset($data['s_tung_num']) ? trim($data['s_tung_num']) : "";
        $arr['s_unit_num'] = isset($data['s_unit_num']) ? trim($data['s_unit_num']) : "";
        $arr['s_agent'] = isset($data['s_agent']) ? trim($data['s_agent']) : "";
        $arr['s_layer_num'] = isset($data['s_layer_num']) ? trim($data['s_layer_num']) : "";
        $arr['s_room_number'] = isset($data['s_room_number']) ? trim($data['s_room_number']) : "";
        $arr['s_money'] = isset($data['s_money']) ? trim($data['s_money']) : "";
        $arr['s_area'] = isset($data['s_area']) ? trim($data['s_area']) : "";
        $arr['s_face_width'] = isset($data['s_face_width']) ? trim($data['s_face_width']) : "";
        $arr['s_layer_depth'] = isset($data['s_layer_depth']) ? trim($data['s_layer_depth']) : "";
        $arr['s_depth'] = isset($data['s_depth']) ? trim($data['s_depth']) : "";
        $arr['s_orientation'] = isset($data['s_orientation']) ? trim($data['s_orientation']) : "";
        $arr['s_floor_plans'] = isset($data['s_floor_plans']) ? implode(',',$data['s_floor_plans']): "";
        $arr['s_indoor_drawings'] = isset($data['s_indoor_drawings']) ? implode(',',$data['s_indoor_drawings']) : "";
        $arr['s_matching'] = isset($data['s_matching']) ? implode(',',$data['s_matching']) : "";
        $arr['s_a_time'] = time();
        $arr['create_id'] = create_id($data['token']);
        $datas=Db::name('shops')->insert($arr);
        //判断是否存为模板
        $data['s_template']= isset($data['s_template']) ? trim($data['s_template']) : "";
        if($data['s_template']==1){
            //处理数据
            //添加操作用户id
            $uid=get_user_id($data['token']);
            $count['s_te_uid'] = $uid;
            //$count['s_te_uid'] = 2;
            $count['s_te_name'] = isset($data['s_name']) ? trim($data['s_name']) : "";
            $count['s_te_money'] = isset($data['s_money']) ? trim($data['s_money']) : "";
            $count['s_te_area'] = isset($data['s_area']) ? trim($data['s_area']) : "";
            $count['s_te_face_widt'] = isset($data['s_face_width']) ? trim($data['s_face_width']) : "";
            $count['s_te_layer_depth'] = isset($data['s_layer_depth']) ? trim($data['s_layer_depth']) : "";
            $count['s_te_depth'] = isset($data['s_depth']) ? trim($data['s_depth']) : "";
            $count['s_te_orientation'] = isset($data['s_orientation']) ? trim($data['s_orientation']) : "";
            $count['s_te_floor_plans'] = isset($data['s_floor_plans']) ? implode(',',$data['s_floor_plans']) : "";
            $count['s_te_indoor_drawings'] = isset($data['s_indoor_drawings']) ? implode(',',$data['s_indoor_drawings']) : "";
            $count['s_te_matching'] = isset($data['s_matching']) ? implode(',',$data['s_matching']) : "";
            $count['s_te_a_time'] = time();
            $count['create_id'] = create_id($data['token']);
            $data=Db::name('shops_template')->insert($count);
        }
        return $datas;
    }
    /**
     * 判断楼层房号是否重复
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function tungs($data){
        //处理数据
        $where['s_tung_num'] = $data['s_tung_num'];
        $where['create_id'] = create_id($data['token']);
        $where['s_unit_num'] = $data['s_unit_num'];
        $where['s_layer_num'] = $data['s_layer_num'];
        $where['s_room_number'] = $data['s_room_number'];
        $where['s_state'] = array('eq',1);
        $date=Db::name('shops')->where($where)->find();
        return $date;
    }
    /**
     * 判断楼层房号是否重复
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function tungsUp($data){
        //处理数据
        $where['s_tung_num'] = $data['s_tung_num'];
        $where['create_id'] = create_id($data['token']);
        $where['s_unit_num'] = $data['s_unit_num'];
        $where['s_layer_num'] = $data['s_layer_num'];
        $where['s_room_number'] = $data['s_room_number'];
        $where['s_state'] = 1;
        $wheres['shops_id'] = array('neq',$data['shops_id']);
        $date=Db::name('shops')->where($wheres)->where($where)->find();
        return $date;
    }
    /**
     * 商铺资源查询
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function shopsOne($id){
        //处理数据
        $data=Db::name('shops')
            ->alias('a')
            ->join('admin_user','a.s_agent=admin_user.admin_id')
            ->where(['shops_id'=>$id])
            ->where(['s_state'=>1])
            ->find();
        $data['s_floor_plans']=explode(',',$data['s_floor_plans']);
        $data['s_indoor_drawings']=explode(',',$data['s_indoor_drawings']);
        $data['matching']=$data['s_matching'];
        $data['s_matching']=explode(',',$data['s_matching']);

        return $data;
    }
    /**
     * 商铺修改
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function shopsUpdate($data){
        //处理数据
        $where['shops_id']=isset($data['shops_id']) ? trim($data['shops_id']) : "";
        $arr['s_tung_num'] = isset($data['s_tung_num']) ? trim($data['s_tung_num']) : "";
        $arr['s_unit_num'] = isset($data['s_unit_num']) ? trim($data['s_unit_num']) : "";
        $arr['s_agent'] = isset($data['s_agent']) ? trim($data['s_agent']) : "";
        $arr['s_layer_num'] = isset($data['s_layer_num']) ? trim($data['s_layer_num']) : "";
        $arr['s_room_number'] = isset($data['s_room_number']) ? trim($data['s_room_number']) : "";
        $arr['s_money'] = isset($data['s_money']) ? trim($data['s_money']) : "";
        $arr['s_area'] = isset($data['s_area']) ? trim($data['s_area']) : "";
        $arr['s_face_width'] = isset($data['s_face_width']) ? trim($data['s_face_width']) : "";
        $arr['s_layer_depth'] = isset($data['s_layer_depth']) ? trim($data['s_layer_depth']) : "";
        $arr['s_depth'] = isset($data['s_depth']) ? trim($data['s_depth']) : "";
        $arr['s_orientation'] = isset($data['s_orientation']) ? trim($data['s_orientation']) : "";
        $arr['s_floor_plans'] = isset($data['s_floor_plans']) ? implode(',',$data['s_floor_plans']) : "";
        $arr['s_indoor_drawings'] = isset($data['s_indoor_drawings']) ? implode(',',$data['s_indoor_drawings']) : "";
        $arr['s_matching'] = isset($data['s_matching']) ? implode(',',$data['s_matching']) : "";
        $arr['s_u_time'] = time();
        $data=Db::name('shops')->where($where)->update($arr);
        return $data;
    }
    /**
     * 商铺显示
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function index(){
        $data = input();
        $page=isset($data['page']) ? trim($data['page']) : 1;
        $number=isset($data['s_room_number']) ? trim($data['s_room_number']) : '';
        $style=isset($data['status']) ? trim($data['status']) : '';
        $state=isset($data['type']) ? trim($data['type']) : '';
        $tung_num=isset($data['tung_num']) ? trim($data['tung_num']) : '';
        $unit_num=isset($data['unit_num']) ? trim($data['unit_num']) : '';
        $room_number=isset($data['room_number']) ? trim($data['room_number']) : '';
      //  $state=isset($data['type']) ? trim($data['type']) : '';
        $industry=isset($data['industry']) ? trim($data['industry']) : '';
        $where['s_state']=1;
        $where['create_id'] = create_id($data['token']);
        if($style!=null){
            $where['status']=$style;
        }
        if($tung_num!=null){
            $where['s_tung_num']=$tung_num;
        }
        if($unit_num!=null){
            $where['s_unit_num']=$unit_num;
        }
        if($room_number!=null){
            $where['s_room_number']=$room_number;
        }
        if($state!=null){
            $where['lease']=$state;
        }
        if($number!=null){
            $where['s_room_number']=$number;
        }
        if($industry!=null){
            $where['b.c_industry']=$industry;
            $where['b.h_type']=2;
            $data=Db::name('shops')
                ->alias('a')
                ->join('contracted_housing b','a.shops_id=b.h_id')
                ->where($where)
                ->field('a.*,b.c_industry')
                ->page($page, 10)
                ->order('s_tung_num asc,s_unit_num asc,s_room_number asc,s_layer_num asc')
                ->select();
            foreach($data as $k=>$v){
                $data[$k]['unit']=$v['s_tung_num'].'-'.$v['s_unit_num'].'-'.$v['s_room_number'];
                $data[$k]['face']='面宽:'.$v['s_face_width'].'层高:'.$v['s_layer_depth'].'进深:'.$v['s_depth'];
                $arr['h_id']=$v['shops_id'];
                $arr['h_type']=2;
                if($v['lease']==2) {
                    $housing = Db::name('contracted_housing')->where($arr)->order('c_h_id desc')->find();
                    $tenant = Db::name('tenant_contract')
                        ->alias('a')
                        ->join('tenant_management b', 'a.t_id=b.tenant_id')
                        ->where(['t_c_mid' => $housing['c_c_mid']])
                        ->field('b.tenant_name')
                        ->where(['t_type' => 1])
                        ->find();
                    $data[$k]['tenant_name'] = $tenant['tenant_name'];//租客
                }
            }
            $count=Db::name('shops')
                ->alias('a')
                ->join('contracted_housing b','a.shops_id=b.h_id')
                ->where($where)
                ->count();
        }else{
            $data=Db::name('shops')
                ->alias('a')
                ->where($where)
                ->field('a.*')
                ->page($page, 10)
                ->order('s_tung_num asc,s_unit_num asc,s_room_number asc,s_layer_num asc')
                ->select();
            foreach($data as $k=>$v){
                $data[$k]['unit']=$v['s_tung_num'].'-'.$v['s_unit_num'].'-'.$v['s_room_number'];
                if($v['lease']==1){
                    $industrys['c_industry']='';
                }else{
                    $industrys=Db::name('contracted_housing')
                        ->where(['h_id'=>$v['shops_id']])
                        ->where(['h_type'=>2])
                        ->field('c_industry')
                        ->order('c_h_id desc')
                        ->find();
                }

                $data[$k]['c_industry']=$industrys['c_industry'];
                $data[$k]['face']='面宽:'.$v['s_face_width'].'层高:'.$v['s_layer_depth'].'进深:'.$v['s_depth'];
                $arr['h_id']=$v['shops_id'];
                $arr['h_type']=2;
                if($v['lease']==2) {
                    $housing = Db::name('contracted_housing')->where($arr)->order('c_h_id desc')->find();
                    $tenant = Db::name('tenant_contract')
                        ->alias('a')
                        ->join('tenant_management b', 'a.t_id=b.tenant_id')
                        ->where(['t_c_mid' => $housing['c_c_mid']])
                        ->field('b.tenant_name')
                        ->where(['t_type' => 1])
                        ->find();
                    $data[$k]['tenant_name'] = $tenant['tenant_name'];//租客
                }
            }
           // var_dump($data);exit;
            $count=Db::name('shops')
                ->alias('a')
                ->where($where)
                ->count();
        }


        $date['data']=$data;
        $date['count']=$count;
        // $data['count']=$count;
        return $date;
    }
    /**
     * 商铺删除
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function ShopsDel($id){
        //处理数据
        $where['shops_id'] = $id;
        $date=Db::name('shops')->where($where)->update(['s_state'=>2]);
        return $date;
    }

    /**
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function indexCron(){
        $where['s_state']=1;
        $where['lease']=2;
        $data=Db::name('shops')
            ->field('shops_id,s_tung_num,s_unit_num,s_layer_num,s_room_number,s_agent, create_id')
            ->where($where)
            ->select();
        return $data;
    }

    /**
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function indexLease($admin_id) {
        $where['lease'] = 2;
        $where['s_state'] = 1;
        $where['create_id'] = $admin_id;
        $data=Db::name('shops')
            ->where($where)
            ->select();
        return $data;
    }

    /**
     * @param $id
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function detail($id) {
        $where['shops_id'] = $id;
        $where['s_state'] = 1;
        $data = Db::name('shops')
            ->where($where)
            ->find();
        return $data;
    }
}